package com.xmh.ssm.utils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DaoBeans {

	private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();

	@Resource(name = "sqlSessionFactory")
	private SqlSessionFactory sessionFactory = null;

	@Autowired
	private static SqlSessionFactory sessionFactory_s = null;

	@PostConstruct
	public void init() {
		sessionFactory_s = sessionFactory;
	}

	public static SqlSession getSession() {
		SqlSession sqlSession = threadLocal.get();
		if (sqlSession == null) {
			SqlSession openSession = sessionFactory_s.openSession();
			threadLocal.set(openSession);
			return openSession;
		}
		return sqlSession;
	}

}
